import allQuotes from '../../quotes.json';

export default (req, res) => {
  //emmm 这俩参数应该是原生的 Next.js文档中 直接给出了NodeJS文档地址 而不是其他框架的地址 作为说明
  //https://nodejs.org/api/http.html#http_class_http_incomingmessage
  //https://nodejs.org/api/http.html#http_class_http_serverresponse
  const { author } = req.query;
  let quotes = allQuotes;

  if (author) {
    quotes = quotes.filter(quote => quote.author.toLowerCase().includes(author.toLowerCase()));
  }
  if (!quotes.length) {
    quotes = allQuotes.filter(quote => quote.author.toLowerCase() === 'unknown');
  }

  const quote = quotes[Math.floor(Math.random() * quotes.length)];

  res.status(200).json(quote);
};

/** 默认不支持跨域*/
